<template>
  <div class="col main-content">
    <div class="row tool-bar">
      <div class="col">
        <el-form :inline="true">
          <el-form-item label="旧买家:">
            <h-user-choose v-model="search.oldBuyerId"/>
          </el-form-item>
          <el-form-item label="新买家:">
            <h-user-choose v-model="search.newBuyerId"/>
          </el-form-item>
          <el-form-item label="申请日期:">
            <h-date-range-picker v-model="search.dates"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchAction"><i class="fa fa-search"></i> 查询</el-button>
          </el-form-item>
          <el-form-item label="数据类型:">
            <el-radio-group v-model="search.renamed" @change="typeChange">
              <el-radio :label="0"> 待更名订单</el-radio>
              <el-radio :label="1"> 已更名订单</el-radio>
            </el-radio-group>
          </el-form-item>

        </el-form>
      </div>
    </div>

    <div class="row">
      <div class="col">
        <el-table
          :data="renames"
          border
          tooltip-effect="dark"
          v-loading="loading"
          @selection-change="handleSelectionChange"
          ref="table"
        >
          <el-table-column
            type="selection"
            width="48" align="center" fixed="left">
          </el-table-column>
          <el-table-column
            label="订单编号" width="120" align="center"
          >
            <template slot-scope="scope">{{ scope.row.order.sn }}</template>
          </el-table-column>
          <el-table-column
            label="新买家" min-width="200" align="center"
          >
            <template slot-scope="scope">
              <h-user-display v-model="scope.row.newBuyer"/>
            </template>
          </el-table-column>


          <el-table-column
            label="旧买家" min-width="200" align="center"
          >
            <template slot-scope="scope">
              <h-user-display v-model="scope.row.oldBuyer"/>
            </template>
          </el-table-column>


          <el-table-column
            label="商品信息" min-width="260">
            <template slot-scope="scope">
              <h-good-info-display v-model="scope.row.order"></h-good-info-display>
            </template>
          </el-table-column>

          <el-table-column label="商品规格" min-width="100">
            <template slot-scope="scope">
              <h-specs-display v-model="scope.row.order"></h-specs-display>
            </template>
          </el-table-column>
          <el-table-column label="下单时间" width="160" prop="createTime" align="center">
          </el-table-column>
          <el-table-column label="订单信息" min-width="200">
            <template slot-scope="scope">
              <h-order-info-display v-model="scope.row.order"></h-order-info-display>
            </template>
          </el-table-column>

          <el-table-column
            label="订单金额"
            min-width="150">
            <template slot-scope="scope">
              <h-order-pay-info-display v-model="scope.row.order"/>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" width="120">
            <template slot-scope="scope">
              <h-order-status-display v-model="scope.row.order" :arrival="true" :pay="true"></h-order-status-display>
            </template>
          </el-table-column>
        </el-table>
        <div class="h-pagination">
          <div class="pagination-tool">
            <el-button v-if="search.renamed==0" type="primary" @click="batchRenamed" :loading="datachanging"> 批量通过更名
            </el-button>
          </div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import HDateRangePicker from 'src/components/common/HDateRangePicker'
  import HUserChoose from 'src/components/common/HUserChoose'
  import AdminService from 'src/service/admin'
  import HOrderStatusDisplay from 'src/components/common/display/HOrderStatusDisplay'
  import HGoodInfoDisplay from 'src/components/common/display/HGoodInfoDisplay'
  import HSpecsDisplay from 'src/components/common/display/HSpecsDisplay'
  import HOrderInfoDisplay from 'src/components/common/display/HOrderInfoDisplay'
  import HOrderPayInfoDisplay from 'src/components/common/display/HOrderPayInfoDisplay'
  import HSpecChoose from '../../common/choose/HSpecChoose'
  import HUserDisplay from '../../common/display/HUserDisplay'
  import TableMix from 'src/components/table-mix'

  export default {
    name: 'AdminOrderRename',
    mixins: [TableMix],
    components: {
      HDateRangePicker, HUserChoose, HOrderStatusDisplay, HGoodInfoDisplay,
      HSpecsDisplay, HOrderInfoDisplay, HOrderPayInfoDisplay,
      HSpecChoose, HUserDisplay
    },
    data() {
      return {
        renames: [],
        search: {
          oldBuyerId: null,
          newBuyerId: null,
          renamed: 0,
          dates: [],
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          }
        },
        selected: [],
        loading: false,
        datachanging: false
      }
    },
    methods: {
      handleSelectionChange: function (v) {
        this.selected = v
      },
      typeChange: function (v) {
        this.search.renamed = v
        this.getDatas()
      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page
        this.getDatas()
      },
      handleSizeChange: function (size) {
        this.search.pagination.limit = size
        this.getDatas()
      },
      //查询操作
      searchAction: function () {
        this.search.pagination.page = 1
        this.getDatas()
      },
      batchRenamed: function () {
        const me = this
        if (this.selected.length == 0) {
          me.$message.warning('请选择数据')
          return
        }
        this.$confirm('此操作将通过选中的更名数据并把订单迁入新的买家?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          me.datachanging = true
          const ids = me.selected.map(d => {
            return d.id
          })
          const d = {
            ids: ids,
            status: 1
          }
          AdminService.renamePass(d).then(ds => {
            if (ds != null) {
              me.$message({
                type: 'success',
                message: '数据变更通过成功!'
              })
              me.searchAction()
            }
            me.datachanging = false
          })
        }).catch(() => {
        })
      },
      getDatas: function () {
        const me = this
        this.loading = true
        AdminService.renameSearch(me.search).then(ds => {
          if (ds != null) {
            me.renames = ds.datas
            me.search.pagination = ds.pagination
          }
          this.loading = false
        })
      }
    },
    mounted: function () {
      //this.initTalbeHeight(390)
      this.getDatas()
    }
  }
</script>
